package com.fish.common.web;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.fish.common.constant.Constant;
import com.fish.common.tag.Pager;
import com.fish.user.po.User;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 功能描述:整个系统Action的基类,其它系统事件都必须继承;<br>
 * 创建人: clj<br>
 * 创建日期：2010.06.5<br>
 * 修改日期：2010.06.5<br>
 * CopyRight(c) 2010 V1.0
 */
public abstract class BaseAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware, SessionAware {

	private static final long serialVersionUID = -1293649277869358203L;
	private static final Logger log = Logger.getLogger(BaseAction.class);
	public static final String SESSION_USER = "user";
	// private LogService logService;
	private Map<String,Object> session;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private List<File> upload;
	// 用String数组来封装多个上传文件名
	private List<String> uploadFileName;
	// 用String数组来封装多个上传文件类型
	private List<String> uploadContentType;

	/**
	 * 分页对象
	 */
	protected Pager pager = new Pager();
	/**
	 * 分页参数
	 */
	protected Map<String, Object> param = new HashMap<String, Object>();
	/**
	 * 当前翻页数
	 */
	protected int page = 1;
	/**
	 * 每页显示条数
	 */
	protected int pageSize = 10;
	/**
	 * 分页总记录数
	 */
	protected int count = 0;
	
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	public User getSessionUser(){
		return (User)getRequest().getSession().getAttribute(Constant.SESSION_BACK_USER_KEY);
	}
	
	public void removeSessionUser() {
		this.session.remove(Constant.SESSION_BACK_USER_KEY);
	}
	
	/**
	 * 获得 HttpServletRequest
	 * 
	 * @return
	 */
	public HttpServletRequest getRequest() {
		return this.request;
	}

	/**
	 * 获得HttpServletResponse
	 * 
	 * @return
	 */
	public HttpServletResponse getResponse() {
		return this.response;
	}

	public void setSession(Map<String,Object> session) {
		this.session = session;
	}

	/**
	 * 设置session
	 * 
	 * @param key
	 * @param value
	 */
	public void setSessionValue(String key, Object value) {
		this.session.put(key, value);
	}

	/**
	 * 获取session
	 * 
	 * @param key
	 * @return
	 */
	public Object getSessionValue(String key) {
		return this.session.get(key);
	}

	/**
	 * 添加处理结果到Request对象
	 * 
	 * @param key
	 * @param value
	 */
	public void addResult(String key, Object value) {
		ServletActionContext.getRequest().setAttribute(key, value);
	}

	/**
	 * 从Request对象里取值
	 * 
	 * @param key
	 * @param value
	 */
	public Object getResult(String key) {
		return ServletActionContext.getRequest().getAttribute(key);
	}

	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public String getRealyPath(String path) {
		return getServletContext().getRealPath(path);
	}

	/**
	 * 生成ajax结果
	 * 
	 * @param json
	 */
	public void ajaxResult(String json) {
		this.getResponse().setContentType("text/html;charset=UTF-8");
		try {
			PrintWriter out = this.getResponse().getWriter();
			out.print(json);
			out.flush();
			out.close();
		} catch (Exception e) {
			log.error("返回Ajax结果异常", e);
		}
	}

	public void sendXmlResult(String result) {
		this.getResponse().setContentType("text/xml;charset=GBK");
		this.getResponse().setCharacterEncoding("GBK");
		try {
			PrintWriter out = this.getResponse().getWriter();
			out.write(result);
			out.flush();
			out.close();
		} catch (Exception e) {
			log.error("返回xml结果异常", e);
		}
	}
	/**
	 * 生成唯一编号
	 * 
	 * @return
	 */
	public String getNewUUID() {
		String uuid = java.util.UUID.randomUUID().toString().replace("-", "");
		return uuid;//32位的例如:b9ff65fcabf249f1a51f3d6e4f3282f0
	}
	
	/**
	 * 生成唯一编号以时间为开始几位
	 * 
	 * @return
	 */
	public String getNewDateUUID() {
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");		
		String dateStr = sdf.format(now);
		String uuid = dateStr+"-"+java.util.UUID.randomUUID().toString().replace("-", "");
		return uuid;//时间+32位的例如:2012-02-01b9ff65fcabf249f1a51f3d6e4f3282f0
	}
	
	/**
	 * 初始化分页参数
	 */
	public void initPagingParam() {
		param.put("startIndex", this.getStartIndex());
		param.put("pageSize", this.pageSize);
	}
	/**
	 * 返回数据库分页开始下标
	 * 
	 * @return
	 */
	protected int getStartIndex() {
		return (page - 1) * pageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
	
	/**
	 * 获取分页对象 
	 */
	public Pager getPager() {
		return pager;
	}

	/**
	 * @param 分页对象 the pager to set
	 */
	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	
	public String getIpAddr() {
		String ip = request.getHeader("x-forwarded-for");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}
